package com.zds.support.api.retrofit.function;



import com.zds.support.api.retrofit.exception.ApiException;
import com.zds.support.api.retrofit.response.ApiResponse;
import io.reactivex.functions.Function;


public class ApiResponseFunction<T> implements Function<ApiResponse<T>, T> {
    @Override
    public T apply(ApiResponse<T> apiResponse) throws Exception {
        if (apiResponse.isSuccess()) {
            T data = apiResponse.getData();
            if (data != null) {
                return data;
            } else {
                throw new ApiException(ExceptionEngine.EMPTY_DATA, "Api response data is null");
            }
        } else {
            throw new ApiException(apiResponse.getCode(), apiResponse.getMsg());
        }
    }
}
